package com.eloo.otsclient.ui;



import java.awt.Container;
import java.awt.Dimension;
import java.awt.GraphicsConfiguration;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.Rectangle;
import java.awt.Toolkit;
import java.awt.event.ActionListener;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;

import com.eloo.otsclient.core.OTSConstants;

public class OTSClientUI {
 
	private static final long serialVersionUID = -6098574587006058177L;

	static ActionListener loginListerner;
	

	private static JTextField jtextLogin = new JTextField("用户名");
	private static JPasswordField jtextPassword = new JPasswordField("密码"); 
	
	private static JTextField jtextStartStation= new JTextField();

	private static JTextField jtextEndStation = new JTextField();
	private static JTextField jtextStartDate = new JTextField(new SimpleDateFormat("yyyy-MM-dd").format(new Date()));
	private static JTextField jtextReturnDate = new JTextField(new SimpleDateFormat("yyyy-MM-dd").format(new Date()));
	private static JTextField jtextQRcode = new JTextField(20);
 
	private static JLabel jlableStartStation = new JLabel("starStation:");
	private static JLabel jlableEndStation= new JLabel("EndStation:"); 

	private static JLabel jlableQRcode = new JLabel("QR code :");

	private static JLabel jlabelViewQR = new JLabel();
	private static JLabel jlableLogin = new JLabel("OTS Loginname :");
	private static JLabel jlableDate = new JLabel("Date:"); 
	private static JLabel jlablePassword = new JLabel("OTS Password :"); 
	private static JButton jbuttonLogin = new JButton(OTSConstants.LOGIN);
	private static JButton jbuttonStartSearch= new JButton(OTSConstants.SEARCH);
	private static JTextArea jtextareaInfo= new JTextArea(200,250);

	private static JScrollPane scrollPane = new JScrollPane(jtextareaInfo,
			JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
			JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);

	static JPanel jpanelMain=null; 
	static JFrame jframeMain = new JFrame(); 
  

	public static JPanel initframe(String titles) {
		jpanelMain= new JPanel(new GridBagLayout());
		int height = Integer.parseInt(380 + "");
		int width = Integer.parseInt(380 + "");
		int x = Integer.parseInt(460 + "");
		int y = Integer.parseInt(460 + "");

		// Inspect user's screens to ensure they can see the frame. Some users
		// switch between 1 and 2
		// screen systems.
		Rectangle virtualBounds = new Rectangle();
		GraphicsEnvironment ge = GraphicsEnvironment
				.getLocalGraphicsEnvironment();
		GraphicsDevice[] gs = ge.getScreenDevices();

		for (int j = 0; j < gs.length; j++) {
			GraphicsDevice gd = gs[j];
			GraphicsConfiguration gc = gd.getDefaultConfiguration();
			virtualBounds = virtualBounds.union(gc.getBounds());
		}

		if (x >= virtualBounds.x && x <= virtualBounds.x + virtualBounds.width
				&& y >= virtualBounds.y
				&& y <= virtualBounds.y + virtualBounds.height) {
			OTSClientUI.jpanelMain.setLocation(x, y);
		}
		Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
		OTSClientUI.jframeMain.setBounds((d.width - width) / 2,(d.height - height) / 2, width, height); // set with height
		OTSClientUI.jframeMain.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		width = Math.min(width, virtualBounds.width);
		height = Math.min(height, virtualBounds.height);
		Container contentPane = OTSClientUI.jframeMain.getContentPane();
		((JPanel) contentPane).setPreferredSize(new Dimension(width, height));
	 
		jpanelMain.add(jlableLogin, new GridBagConstraints(0, 0, 1, 1, 1.0,
				0.0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL,
				new Insets(2, 2, 2, 0), 0, 0));

		jpanelMain.add(jtextLogin, new GridBagConstraints(1, 0, 1, 1, 1.0,
				0.0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL,
				new Insets(2, 2, 2, 0), 0, 0));

		jpanelMain.add(jlablePassword, new GridBagConstraints(0, 1, 1, 1,
				1.0, 0.0, GridBagConstraints.WEST,
				GridBagConstraints.HORIZONTAL, new Insets(2, 2, 2, 0), 0, 0));
		jpanelMain.add(jtextPassword, new GridBagConstraints(1, 1, 1, 1, 1.0,
				0.0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL,
				new Insets(2, 2, 2, 0), 0, 0)); 
		jpanelMain.add(jbuttonLogin, new GridBagConstraints(0, 2, 1, 1, 1.0,
				0.0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL,
				new Insets(2, 2, 2, 0), 0, 0));

		jpanelMain.add(jbuttonStartSearch, new GridBagConstraints(1, 2, 1, 1,
				1.0, 0.0, GridBagConstraints.WEST,
				GridBagConstraints.HORIZONTAL, new Insets(2, 2, 2, 0), 0, 0));
		jpanelMain.add(jlableQRcode, new GridBagConstraints(0, 3, 1, 1, 1.0,
				0.0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL,
				new Insets(2, 2, 2, 0), 0, 0));
		jpanelMain.add(jtextQRcode, new GridBagConstraints(1, 3, 1, 1, 1.0,
				0.0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL,
				new Insets(2, 2, 2, 0), 0, 0));
		jlabelViewQR.setSize(200, 200);
		jpanelMain.add(jlabelViewQR, new GridBagConstraints(0, 4, 2, 1, 1.0,
				0.0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL,
				new Insets(2, 2, 2, 0), 0, 0));
		
		jtextareaInfo.setSize(100, 100);  
		jtextareaInfo.setEditable(false);
		
		scrollPane.setSize(100,100);
		
		jpanelMain.add(scrollPane, new GridBagConstraints(0, 5, 2, GridBagConstraints.REMAINDER, 1.0,
				1.0, GridBagConstraints.NORTHEAST, GridBagConstraints.BOTH,
				new Insets(2, 2, 2, 0), 0, 0));
		

	
		
		contentPane.add(jpanelMain);
		OTSClientUI.jframeMain.setTitle(titles);
		OTSClientUI.jframeMain.pack();
		OTSClientUI.jframeMain.setVisible(true);
		return OTSClientUI.jpanelMain;

	}

	
	
	public static void updateQueryUI(){
		jpanelMain.add(jlableStartStation, new GridBagConstraints(0, 4, 1, 1, 1.0,
				0.0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL,
				new Insets(2, 2, 2, 0), 0, 0));
		jpanelMain.add(jtextStartStation, new GridBagConstraints(1, 4, 1, 1,
				1.0, 0.0, GridBagConstraints.WEST,
				GridBagConstraints.HORIZONTAL, new Insets(2, 2, 2, 0), 0, 0)); 
		jpanelMain.add(jlableEndStation, new GridBagConstraints(0, 5, 1, 1, 1.0,
				0.0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL,
				new Insets(2, 2, 2, 0), 0, 0));
		jpanelMain.add(jtextEndStation, new GridBagConstraints(1, 5, 1, 1,
				1.0, 0.0, GridBagConstraints.WEST,
				GridBagConstraints.HORIZONTAL, new Insets(2, 2, 2, 0), 0, 0));
		
		
		
		jpanelMain.add(jlableDate, new GridBagConstraints(0, 6, 1, 1, 1.0,
				0.0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL,
				new Insets(2, 2, 2, 0), 0, 0));
		jpanelMain.add(jtextStartDate, new GridBagConstraints(1, 6, 1, 1,
				1.0, 0.0, GridBagConstraints.WEST,
				GridBagConstraints.HORIZONTAL, new Insets(2, 2, 2, 0), 0, 0));
		
		jpanelMain.add(jlableQRcode, new GridBagConstraints(0, 7, 1, 1, 1.0,
				0.0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL,
				new Insets(2, 2, 2, 0), 0, 0));
		
		jpanelMain.add(jtextQRcode, new GridBagConstraints(1, 7, 1, 1, 1.0,
				0.0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL,
				new Insets(2, 2, 2, 0), 0, 0));
		jpanelMain.add(jlabelViewQR, new GridBagConstraints(0, 8, 2, 1, 1.0,
				0.0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL,
				new Insets(2, 2, 2, 0), 0, 0));
	

		jtextareaInfo.setSize(100, 100);  
		jtextareaInfo.setEditable(false); 
		scrollPane.setSize(100,100);
		
		jpanelMain.add(scrollPane, new GridBagConstraints(0, 9, 2, GridBagConstraints.REMAINDER, 1.0,
				1.0, GridBagConstraints.NORTHEAST, GridBagConstraints.BOTH,
				new Insets(2, 2, 2, 0), 0, 0));
		
	}
	
	
	
	  

	public static ActionListener getLoginListerner() {
		return loginListerner;
	}

	public static void setLoginListerner(ActionListener loginListerner) {
		OTSClientUI.loginListerner = loginListerner;
	}

	public static JTextField getJtextLogin() {
		return jtextLogin;
	}

	public static void setJtextLogin(JTextField jtextLogin) {
		OTSClientUI.jtextLogin = jtextLogin;
	}

	public static JPasswordField getJtextPassword() {
		return jtextPassword;
	}

	public static void setJtextPassword(JPasswordField jtextPassword) {
		OTSClientUI.jtextPassword = jtextPassword;
	}

	public static JTextField getJtextStartDate() {
		return jtextStartDate;
	}

	public static void setJtextStartDate(JTextField jtextStartDate) {
		OTSClientUI.jtextStartDate = jtextStartDate;
	}

	public static JTextField getJtextReturnDate() {
		return jtextReturnDate;
	}

	public static void setJtextReturnDate(JTextField jtextReturnDate) {
		OTSClientUI.jtextReturnDate = jtextReturnDate;
	}

	public static JTextField getJtextQRcode() {
		return jtextQRcode;
	}

	public static void setJtextQRcode(JTextField jtextQRcode) {
		OTSClientUI.jtextQRcode = jtextQRcode;
	}

	public static JLabel getJlableQRcode() {
		return jlableQRcode;
	}

	public static void setJlableQRcode(JLabel jlableQRcode) {
		OTSClientUI.jlableQRcode = jlableQRcode;
	}

	public static JLabel getJlabelViewQR() {
		return jlabelViewQR;
	}

	public static void setJlabelViewQR(JLabel jlabelViewQR) {
		OTSClientUI.jlabelViewQR = jlabelViewQR;
	}

	public static JLabel getJlableLogin() {
		return jlableLogin;
	}

	public static void setJlableLogin(JLabel jlableLogin) {
		OTSClientUI.jlableLogin = jlableLogin;
	}

	public static JLabel getJlableDate() {
		return jlableDate;
	}

	public static void setJlableDate(JLabel jlableDate) {
		OTSClientUI.jlableDate = jlableDate;
	}

	public static JLabel getJlablePassword() {
		return jlablePassword;
	}

	public static void setJlablePassword(JLabel jlablePassword) {
		OTSClientUI.jlablePassword = jlablePassword;
	}

	public static JButton getJbuttonLogin() {
		return jbuttonLogin;
	}

	public static void setJbuttonLogin(JButton jbuttonLogin) {
		OTSClientUI.jbuttonLogin = jbuttonLogin;
	}

	public static JButton getJbuttonStartSearch() {
		return jbuttonStartSearch;
	}

	public static void setJbuttonStartSearch(JButton jbuttonStartSearch) {
		OTSClientUI.jbuttonStartSearch = jbuttonStartSearch;
	}

	public static JPanel getJpanelMain() {
		return jpanelMain;
	}

	public static void setJpanelMain(JPanel jpanelMain) {
		OTSClientUI.jpanelMain = jpanelMain;
	}

	public static JFrame getJframeMain() {
		return jframeMain;
	}

	public static void setJframeMain(JFrame jframeMain) {
		OTSClientUI.jframeMain = jframeMain;
	}
	public static JTextArea getJtextareaInfo() {
		return jtextareaInfo;
	}
 
	public static void setJtextareaInfo(JTextArea jtextareaInfo) {
		OTSClientUI.jtextareaInfo = jtextareaInfo;
	}

	public static JTextField getJtextStartStation() {
		return jtextStartStation;
	}

	public static void setJtextStartStation(JTextField jtextStartStation) {
		OTSClientUI.jtextStartStation = jtextStartStation;
	}

	public static JTextField getJtextEndStation() {
		return jtextEndStation;
	}

	public static void setJtextEndStation(JTextField jtextEndStation) {
		OTSClientUI.jtextEndStation = jtextEndStation;
	}

}
